package util.BReceiverUtil;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import com.example.administrator.text.R;
import com.example.administrator.text.WelComeActvity;
import org.apache.log4j.Logger;
import app.MyApplication;
import data.SharedPreferencesUtils;
import util.GreenTreeNetworkUtil;
import util.HomeUtil;
import util.LogUtil;
import util.RequestDataCallback;
import util.StringUtil;
import util.Url;
import util.serviceUtil.MyService;

/**
 * Created by liuxiaobin on 2017/2/12
 * 定时提醒的通知
 */

public class AlarmReceiver extends BroadcastReceiver{
    private Logger mLogger = Logger.getLogger(HomeUtil.class);
    private NotificationManager mManger;
    private Intent mInten;
    private PendingIntent mPendingIntent;
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent != null){
            String typeSring = intent.getStringExtra("networking");
            LogUtil.eE("Service","..来广播了...."+typeSring);
            if(typeSring !=null && typeSring.equals("networkingNormal")){
                Intent inten = new Intent(context,WelComeActvity.class);
                inten.putExtra("state","Notification");
                PendingIntent pendingIntent = PendingIntent.getActivity(context,0,inten,PendingIntent.FLAG_CANCEL_CURRENT);
                if(mManger == null){ mManger = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                }
                String monryTime = (String)SharedPreferencesUtils.getParam(context,HomeUtil.mMoneyTime,"");  //上网的时间
                int time = (int)SharedPreferencesUtils.getParam(context,HomeUtil.mNetworkingTime,0);        //预约的时长
                int tiemInt = (int) StringUtil.getStringUtilNew().compareTime(monryTime,time);
                LogUtil.eE("","........BroadcastReceiver.........收到时间通知广播.."+tiemInt+".......time...."+time);
                mLogger.info("........BroadcastReceiver.........收到时间通知广播.."+tiemInt);
                if(tiemInt < 10){
                    Notification notifation = new Notification.Builder(context)
                            .setContentTitle("温馨提示")
                            .setContentText("您预约NewFi的网络即将结束可重新联网")
                            .setSmallIcon(R.mipmap.app)
                            .setDefaults(Notification.DEFAULT_ALL)
                            .setContentIntent(pendingIntent)
                            .setAutoCancel(true)
                            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.app))
                            .build();
                    mManger.notify(0,notifation);
                    MyApplication.getApp().setFWQString((String)SharedPreferencesUtils.getParam(context,HomeUtil.mFWQ,""));
                    mLogger.info("........BroadcastReceiver.........发出一个广播....");
                }else {
                    Intent inte = new Intent(context,MyService.class);
                    inte.putExtra("type","ok");
                    context.startService(inte);
                }
            }else if(typeSring !=null && typeSring.equals("updateNetwork")){
                getNetworkSatae(context);
//                Intent inten = new Intent(context,MyService.class);
//                inten.putExtra("type","network");
//                context.startService(inten);
            }else if(typeSring !=null && typeSring.equals("updateNetwork66")){
                getCancelNotice(context);
            }else if(typeSring !=null && typeSring.equals("updateNetwork6")){
                getNetworkSatae(context);
            }
        }
    }

    /**
     * 获取网络状态
     */
    private void getNetworkSatae(final Context context){
        if(MyApplication.getApp().getSiteAreaid() == 0){
            return;
        }
        GreenTreeNetworkUtil.getInstance().doPost(context,"http://123.207.110.98:8080/wifi/ping.html",2000,null,new RequestDataCallback() {
            @Override public void onSuccess(String response) {
                updateNotice(context,response);
            }

            @Override public void onFailure(Throwable error) {
                updateNotice(context,"ghfdgh");
            }
        });
    }

    /**
     * 更新通知
     */
    private void updateNotice(Context context,String state){
        String tile;
        int length = state.length();
        LogUtil.eE("Service","......tile......"+length+"..state.."+Url.getUrlNew().getNotifation_state());
        if(length == 2 || length == 1){
                getCancelNotice(context);
                return;
        }else {
            if(MyApplication.getApp().getSiteAreaid() >0 ){
                    tile ="没有连接网络,请去联网认证";
                    LogUtil.eE("Service","..无网...1.");
            }else {
                LogUtil.eE("Service","......SiteAreaid...b..."+MyApplication.getApp().getSiteAreaid());
                return;
            }

        }
        LogUtil.eE("Service","..通知...."+Url.getUrlNew().getNotifation_state());
        if(mInten == null){
            mInten = new Intent(context,WelComeActvity.class);
            mInten.putExtra("state","Notification");
        }
        if(mManger == null){ mManger = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        if(mPendingIntent == null){
            mPendingIntent = PendingIntent.getActivity(context,0,mInten,PendingIntent.FLAG_CANCEL_CURRENT);
        }
        Notification notifation = new Notification.Builder(context)
                .setContentTitle("温馨提示")
                .setContentText(tile)
                .setSmallIcon(R.mipmap.app)
                .setContentIntent(mPendingIntent)
                .setAutoCancel(true)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.app))
                .build();
        mManger.notify(2693,notifation);
    }

    private void getCancelNotice(Context context){
        if(mManger == null){ mManger = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        mManger.cancel(2693);
        Url.getUrlNew().setNotifation_state(1);
    }
}
